﻿using System;
using System.Linq;
using System.Xml.Serialization;
using Startup.DataContract.Basic;
using Startup.DataContract.Int32;
using Startup.DataContract.String;
using Startup.MVVM;
using Startup.Project.Classes;

namespace Startup.Shell.Domain
{
	[XmlInclude(typeof(MaleNameContract))]
	[XmlInclude(typeof(FemaleNameContract))]
	[XmlInclude(typeof(LastNameContract))]
	[XmlInclude(typeof(HostContract))]
	[XmlInclude(typeof(PortContract))]
	public class StartupProperty : ViewModelBase, INamedItem
	{
		private string _type;
		public string Type
		{
			get { return _type; }
			set
			{
				_type = value;
				RaisePropertyChanged("AvailableContracts");
			}
		}

		private bool _isCollection;
		public bool IsCollection
		{
			get { return _isCollection; }
			set
			{
				_isCollection = value;
				RaisePropertyChanged("AvailableContracts");
			}
		}

		public string Name { get; set; }
		public Guid Id { get; set; }
		public DataContractBase DataContract { get; set; }

		public StartupProperty(StartupProperty source)
			: this()
		{
			Type = source.Type;
			IsCollection = source.IsCollection;
			Name = source.Name;
			Id = source.Id;
			DataContract = source.DataContract;
		}

		public StartupProperty()
		{
			Id = Guid.NewGuid();
		}

		public DataContractBase[] AvailableContracts
		{
			get
			{
				Type dataType = System.Type.GetType(Type);
			
				if (dataType == null)
					return new DataContractBase[0];

				return DataContractManager.Instance.SettingsCollection.Where(x => x.GetDataType() == dataType).ToArray();
			}
		}

		public override string ToString()
		{
			return Name;
		}
	}
}
